package cn.dnuihyy406.dnuifood_16110100406.controller.mefragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.UserImformationModel;

public class UpdateUserActivity extends AppCompatActivity implements OnRetrofitListener<Object> {
    private EditText uname,upassword,uphonenum,uaddress;
    private Button uchange;
    SharedPreferences sharedPreferences;
    String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);
        initViews();
        initData();
        initEvents();
    }


    @Override
    public void onSuccess(Object object, int flag) {
        if(((ResultBean)object).getSuccess().equals("1")){
            Toast.makeText(UpdateUserActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }
        else{
            Toast.makeText(UpdateUserActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(UpdateUserActivity.this,msg,Toast.LENGTH_SHORT).show();
    }

    void initViews(){
        uname=findViewById(R.id.u_name);
        upassword=findViewById(R.id.u_password);
        uaddress=findViewById(R.id.u_address);
        uphonenum=findViewById(R.id.u_phonenum);
        uchange=findViewById(R.id.u_change);
    }
    void initData(){
        sharedPreferences=getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id=sharedPreferences.getString("userid","");
    }

    void initEvents(){
        uchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }
    void updateData(){
        UserImformationModel userImformationModel=new UserImformationModel();
        userImformationModel.updateUserById(user_id,uname.getText().toString(),upassword.getText().toString(),
                uphonenum.getText().toString(),uaddress.getText().toString(),this);
    }
}
